package com.summer.safe.utils;

import com.sun.javafx.PlatformUtil;
import org.beetl.core.Configuration;
import org.beetl.core.GroupTemplate;
import org.beetl.core.Template;
import org.beetl.core.resource.ClasspathResourceLoader;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * 项目模板生成 引擎
 *
 */
public abstract class TemplateEngine {

    protected GroupTemplate groupTemplate;

    protected GenerateConfig config;

    public TemplateEngine(GenerateConfig config) {
        this.config = config;
        initBeetlEngine();
    }

    public void initBeetlEngine() {
        Properties properties = new Properties();
        properties.put("RESOURCE.root", "");
        properties.put("DELIMITER_STATEMENT_START", "<%");
        properties.put("DELIMITER_STATEMENT_END", "%>");
        properties.put("HTML_TAG_FLAG", "##");
        Configuration cfg = null;
        try {
            cfg = new Configuration(properties);
        } catch (IOException e) {
            e.printStackTrace();
        }
        ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader();
        groupTemplate = new GroupTemplate(resourceLoader, cfg);
        groupTemplate.registerFunctionPackage("tool", new ToolUtil());
    }

    public void configTemplate(Template template) {
        template.binding("imports", config.getImportNames());
        template.binding("gen", config.getGen());
        template.binding("scheme", config.getScheme());
        template.binding("columnList", config.getTableColumnList());
    }

    public void generateFile(String template, String filePath) {
        Template pageTemplate = groupTemplate.getTemplate(template);
        configTemplate(pageTemplate);
        if (PlatformUtil.isWindows()) {
            filePath = filePath.replaceAll("/+|\\\\+", "\\\\");
        } else {
            filePath = filePath.replaceAll("/+|\\\\+", "/");
        }
        File file = new File(filePath);
        File parentFile = file.getParentFile();
        if (!parentFile.exists()) {
            parentFile.mkdirs();
        }
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(file);
            pageTemplate.renderTo(fos);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void start() {
        generateEntity();
        generateDao();
        generateService();
        generateController();
        generateXml();
        generatePageFormHtml();
        generatePageListHtml();

        generatePageViewHtml();


        /*generateFormjs();
        generateListjs();*/

    }


    /**
     * Controller层
     */
    protected abstract void generateController();

    /**
     * Dao层
     */
    protected abstract void generateDao();

    /**
     * Service层
     */
    protected abstract void generateService();

    /**
     * Entity层
     */
    protected abstract void generateEntity();

    /**
     * Xml
     */
    protected abstract void generateXml();

    /**
     * Form页面
     */
    protected abstract void generatePageFormHtml();

    /**
     * List页面
     */
    protected abstract void generatePageListHtml();

    /**
     * View页面
     */
    protected abstract void generatePageViewHtml();

    /*protected abstract void generateFormjs();

    protected abstract void generateListjs();*/


}
